package com.juyu.miliao.api.service;

import com.juyu.miliao.common.constant.CallTimeRankingListKey;
import com.juyu.miliao.common.constant.DiamondRankingListKey;
import com.juyu.miliao.common.constant.SpendSystemMessage;
import com.juyu.miliao.common.util.MessageSystemUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Iterator;
import java.util.Set;

/**
 * 通话与钻石排行榜发送消息定时任务
 * @author xuanqicheng
 */
@Service
@Slf4j
public class CallTimeAndDiamondRankingSpendMsgService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "0 10 0 * * ?")
    public void sendMsgByDay(){
        try {
            callTimeRankingSpendMsgByDay();
        }catch (Exception ex){
            log.error("发送通话时长日榜消息错误："+ex.getMessage());
        }
        try {
            diamonRankingSpendMsgByDay();

        }catch (Exception ex){
            log.error("发送钻石日榜消息错误:"+ ex.getMessage());
        }

    }

    /**
     * 砖石日榜发送消息
     */
    private void diamonRankingSpendMsgByDay() {
        //获取钻石日排行榜发送系统消息
        String diamondKey = DiamondRankingListKey.getDayRankingListKey(LocalDate.now().minusDays(1));
        Set<ZSetOperations.TypedTuple<String>> typedTuples1 = stringRedisTemplate.opsForZSet().reverseRangeWithScores(diamondKey, 0, 49);
        Iterator<ZSetOperations.TypedTuple<String>> iterator1 = typedTuples1.iterator();
        //便历排行榜 获得主播userId 发放系统消息
        while (iterator1.hasNext()) {
            ZSetOperations.TypedTuple<String> typedTuple = iterator1.next();
            String userId = typedTuple.getValue();
            Long reverseRank = stringRedisTemplate.opsForZSet().reverseRank(diamondKey, userId);
            String callTimeMessage = SpendSystemMessage.DIAMOND_RANKING_DAY_SEND_MESSAGE.getMessage(reverseRank);
            MessageSystemUtil.sendCustomSystemMessage(Long.parseLong(userId),callTimeMessage);
        }
    }

    /**
     * 通话时长日榜发送消息
     */
    private void callTimeRankingSpendMsgByDay() {
        //获取通话时长日排行榜发送系统消息
        String callTimeKey = CallTimeRankingListKey.getDayRankingListKey(LocalDate.now().minusDays(1));
        Set<ZSetOperations.TypedTuple<String>> typedTuples = stringRedisTemplate.opsForZSet().reverseRangeWithScores(callTimeKey, 0, 49);
        Iterator<ZSetOperations.TypedTuple<String>> iterator = typedTuples.iterator();
        //便历排行榜 获得主播userId 发放系统消息
        while (iterator.hasNext()) {
            ZSetOperations.TypedTuple<String> typedTuple = iterator.next();
            String userId = typedTuple.getValue();
            Long reverseRank = stringRedisTemplate.opsForZSet().reverseRank(callTimeKey, userId);
            String callTimeMessage = SpendSystemMessage.CALL_TIME_RANKING_DAY_SEND_MESSAGE.getMessage(reverseRank) ;
            MessageSystemUtil.sendCustomSystemMessage(Long.parseLong(userId),callTimeMessage);
        }
    }

    @Scheduled(cron = "0 10 0 ? * MON")
    public void sendMsgByWeek(){
        try {
            callTimeRankingSpendMsgByWeek();
        }catch (Exception ex){
            log.error("发送通话时长周榜消息错误:"+ex.getMessage());
        }
        try {
            diamondRankingSpendMsgByWeek();
        }catch (Exception ex){
            log.error("发送钻石日榜消息错误:"+ex.getMessage());
        }

    }

    /**
     * 钻石周榜发送消息
     */
    private void diamondRankingSpendMsgByWeek() {
        //获取钻石排行榜发送系统消息
        String diamondWeekKey = DiamondRankingListKey.getWeekRankingListKey(LocalDate.now().minusWeeks(1));
        Set<ZSetOperations.TypedTuple<String>> diamondTypedTuplesWeek = stringRedisTemplate.opsForZSet().reverseRangeWithScores(diamondWeekKey, 0, 49);
        Iterator<ZSetOperations.TypedTuple<String>> iterator1 = diamondTypedTuplesWeek.iterator();
        //便历排行榜 获得主播userId 发放系统消息
        while (iterator1.hasNext()) {
            ZSetOperations.TypedTuple<String> typedTuple = iterator1.next();
            String userId = typedTuple.getValue();
            Long reverseRank = stringRedisTemplate.opsForZSet().reverseRank(diamondWeekKey, Long.parseLong(userId));
            String callTimeMessage = SpendSystemMessage.DIAMOND_RANKING_WEEK_SEND_MESSAGE.getMessage(reverseRank);
            MessageSystemUtil.sendCustomSystemMessage(Long.parseLong(userId),callTimeMessage);
        }
    }

    /**
     * 通话时长周榜发送消息
     */
    private void callTimeRankingSpendMsgByWeek() {
        String callTimeWeekKey = CallTimeRankingListKey.getWeekRankingListKey(LocalDate.now().minusWeeks(1));
        Set<ZSetOperations.TypedTuple<String>> callTypedTupleWeek = stringRedisTemplate.opsForZSet().reverseRangeWithScores(callTimeWeekKey, 0, 49);
        Iterator<ZSetOperations.TypedTuple<String>> iterator = callTypedTupleWeek.iterator();
        //便历排行榜 获得主播userId 发放系统消息
        while (iterator.hasNext()) {
            ZSetOperations.TypedTuple<String> typedTuple = iterator.next();
            String userId = typedTuple.getValue();
            Long reverseRank = stringRedisTemplate.opsForZSet().reverseRank(callTimeWeekKey, Long.parseLong(userId));
            String callTimeMessage = SpendSystemMessage.CALL_TIME_RANKING_WEEK_SEND_MESSAGE.getMessage(reverseRank);
            MessageSystemUtil.sendCustomSystemMessage(Long.parseLong(userId),callTimeMessage);
        }
    }
}
